package com.xx.pmis.web.actions;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Stock;
import com.xx.pmis.services.StockService;
import com.xx.pmis.web.forms.StockForm;
import com.xx.pmis.web.util.CurrentDate;

/**
 * @class 股票Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class StockAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(StockAction.class);

	StockService stockService = getPMISServices().getStockService();

	// 查询所有股票
	public ActionForward listStock(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<Stock> all = null;
		try {
			all = stockService.getAllStock();
		} catch (Exception e) {
			String errorPage = "【财务管理/股票】查询【股票】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		List<Stock> list = new ArrayList<Stock>();
		for(Stock stock : all) {
			double ykje = Double.parseDouble(stock.getStockGb()) - Double.parseDouble(stock.getStockGb());
			if(ykje>=0) {
				stock.setYk("盈");
			} else {
				stock.setYk("亏");
			}
			stock.setYkje(String.valueOf(ykje));
			//list.add(stock);
		}
		request.setAttribute("stockList", list);
		return mapping.findForward("listStock");
	}

	// 新增
	public ActionForward addStock(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("stockDate", CurrentDate.currentDateAndTime());
		request.setAttribute("position", "股票/新增");
		return mapping.findForward("addStock");
	}

	// 新增，处理方法
	public ActionForward addStock_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		StockForm f = (StockForm) form;

		String stockGb = f.getStockGb(); // 股本
		String stockZj = f.getStockZj(); // 资金
		String stockDate = f.getStockDate(); // 记录日期
		String stockDesc = f.getStockDesc();
		Stock stock = new Stock();
		List<Stock> all = null;
		try {
			all = stockService.getAllStock();
		} catch (Exception e) {
			String errorPage = "【财务管理/股票】查询【股票】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		if (all.size() == 0) {
			stock.setStockGb(stockGb);
			if (stockZj.equals("") || stockZj.equals(null)) {
				stockZj = "0";
			}
			stock.setStockZj(stockGb);

		} else {
			Stock lastStock = all.get(0);
			if (stockZj.equals("") || stockZj.equals(null)) {
				stockZj = "0";
			}
			if (stockGb.equals("") || stockGb.equals(null)) {
				// 股本*等于*上一期股票
				stock.setStockGb(lastStock.getStockGb());
				stock.setStockZj(stockZj);
			} else if (stockGb.startsWith("-")) {
				// 上期股本*减去*追加股本
				stock.setStockGb(String.valueOf(Double.parseDouble(lastStock.getStockGb())
						- Double.parseDouble(stockGb.replace("-", ""))));
				stock.setStockZj(String.valueOf(Double.parseDouble(lastStock.getStockZj())
						- Double.parseDouble(stockGb.replace("-", ""))));
			} else {
				// 追加股本*加上*上期股本
				stock.setStockGb(String.valueOf(Double.parseDouble(lastStock.getStockGb())
						+ Double.parseDouble(stockGb)));
				stock.setStockZj(String.valueOf(Double.parseDouble(lastStock.getStockZj())
						+ Double.parseDouble(stockGb)));
			}
		}

		stock.setStockDate(stockDate);
		stock.setStockDesc(stockDesc);

		try {
			stockService.saveOrUpdateStock(stock);
		} catch (Exception e) {
			String errorPage = "【财务管理/股票/新增】保存或更新【股票】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}

		listStockByPage(mapping, form, request, response);
		return mapping.findForward("listStock");
	}

//	// 编辑
//	public ActionForward editStock(ActionMapping mapping, ActionForm form, HttpServletRequest request,
//			HttpServletResponse response) {
//		String stockID = request.getParameter("stockID");
//		Stock stock = null;
//		try {
//			stock = stockService.getStockByStockID(stockID);
//		} catch (Exception e) {
//			String errorPage = "【财务管理/股票/编辑】根据【股票ID】查询该【股票】异常！" + e.toString();
//			log.info(errorPage);
//			request.setAttribute("errorPage", errorPage);
//			request.setAttribute("position", "出错了");
//			return mapping.findForward("errorPage");
//		}
//		request.setAttribute("stock", stock);
//		request.setAttribute("position", "股票/编辑");
//		return mapping.findForward("editStock");
//	}
//
//	// 编辑，处理方法
//	public ActionForward editStock_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
//			HttpServletResponse response) {
//		StockForm f = (StockForm) form;
//		String stockID = f.getStockID();
//		String stockGb = f.getStockGb(); // 股本
//		String stockZj = f.getStockZj(); // 资金
//		String stockDate = f.getStockDate(); // 记录日期
//		String stockDesc = f.getStockDesc();
//
//		Stock stock = null;
//		try {
//			stock = stockService.getStockByStockID(stockID);
//		} catch (Exception e) {
//			String errorPage = "【财务管理/股票/编辑】根据【股票ID】查询该【股票】异常！" + e.toString();
//			log.info(errorPage);
//			request.setAttribute("errorPage", errorPage);
//			request.setAttribute("position", "出错了");
//			return mapping.findForward("errorPage");
//		}
//		stock.setStockGb(stockGb);
//		stock.setStockZj(stockZj);
//		stock.setStockDate(stockDate);
//		stock.setStockDesc(stockDesc);
//
//		try {
//			stockService.saveOrUpdateStock(stock);
//		} catch (Exception e) {
//			String errorPage = "【财务管理/股票/编辑】保存或更新【股票】异常！" + e.toString();
//			log.info(errorPage);
//			request.setAttribute("errorPage", errorPage);
//			request.setAttribute("position", "出错了");
//			return mapping.findForward("errorPage");
//		}
//
//		listStockByPage(mapping, form, request, response);
//		return mapping.findForward("listStock");
//	}

	// 删除
	public ActionForward delStock(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Stock stock = null;
		StockForm f = (StockForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				stock = stockService.getStockByStockID(ids[i]);
			} catch (Exception e) {
				String errorPage = "【财务管理/股票/删除】根据【股票ID】查询该【股票】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
			try {
				stockService.deleteStock(stock);
			} catch (Exception e) {
				String errorPage = "【财务管理/股票/删除】删除【股票】异常！" + e.toString();
				log.info(errorPage);
				request.setAttribute("errorPage", errorPage);
				request.setAttribute("position", "出错了");
				return mapping.findForward("errorPage");
			}
		}

		listStockByPage(mapping, form, request, response);
		return mapping.findForward("listStock");
	}

	// 分页显示
	public ActionForward listStockByPage(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		int PAGESIZE = 20;// 每页记录数
		int totalPage = 1;// 总页数

		// 查询总用户记录数
		int count = 0;
		try {
			count = stockService.getCountOfStock();
		} catch (Exception e) {
			String errorPage = "【财务管理/股票】查询【股票】数目异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		request.setAttribute("count", count);
		if (count % PAGESIZE == 0) {
			totalPage = count / PAGESIZE;
		} else {
			totalPage = count / PAGESIZE + 1;
		}

		request.setAttribute("totalPage", totalPage);
		// 声明要翻的页
		String pageNum = "1";
		try {
			String thisPageNum = request.getParameter("pageNum");
			// 如果传了页码参数
			if (thisPageNum != null && thisPageNum.length() != 0) {
				pageNum = thisPageNum;
			}
		} catch (RuntimeException e) {
			log.info("" + e.toString());
		}
		log.info("page:" + pageNum);
		request.setAttribute("pageNum", pageNum);
		List<Stock> all = null;
		try {
			all = stockService.getAllStockByPage(Integer.parseInt(pageNum), PAGESIZE);
		} catch (Exception e) {
			String errorPage = "【财务管理/股票】查询【股票】异常！" + e.toString();
			log.info(errorPage);
			request.setAttribute("errorPage", errorPage);
			request.setAttribute("position", "出错了");
			return mapping.findForward("errorPage");
		}
		List<Stock> list = new ArrayList<Stock>();
		for(Stock stock : all) {
			double ykje = Double.parseDouble(stock.getStockZj()) - Double.parseDouble(stock.getStockGb());
			if(ykje>=0) {
				stock.setYk("盈");
			} else {
				stock.setYk("亏");
			}
			stock.setYkje(String.valueOf(new DecimalFormat("####.##").format(ykje)));
		    list.add(stock);
		}
		request.setAttribute("stockList", list);
		request.setAttribute("PAGESIZE", PAGESIZE);
		// 如果要翻的这页是第一页
		if ("1".equals(pageNum)) {
			// 设置noUp为yes(界面上用此控制'首页'与'上一页'按钮失效)
			request.setAttribute("noUp", "yes");
		}

		// 如果要翻的这页是最后页
		if (totalPage == Integer.parseInt(pageNum)) {
			// 设置noDown为yes(界面上用此控制'下一页'与'尾页'按钮失效)
			request.setAttribute("noDown", "yes");
		}

		request.setAttribute("action", "stock.do");
		request.setAttribute("position", "财务管理/股票");
		return mapping.findForward("listStock");
	}

}